home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include "sw.h"
- #include "extern.h"
- #include "control.h"
- #include "main.h"
- #include "ship.h"
- #include "hud.h"
- #include "resources.h"
- #include "universe.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <Xm/Form.h>
- #include <Xm/RowColumn.h>
- #include <Xm/LabelG.h>
- #include <Xm/Frame.h>
- #include <Xm/PushBG.h>
- #include <Xm/ToggleBG.h>
- #include <Xm/ArrowBG.h>
- #include <Xm/Text.h>
- #include <Xm/ScrolledW.h>
- #include <X11/keysym.h>
- #include <X11/Xirisw/GlxMDraw.h>
- #include <gl/gl.h>
- #include <gl/glws.h>
-
- #define NORADIO
-
- struct GlxSize {
- public:
- Dimension dx, dy; // widget size
- void (*draw)(GlxSize*, int); // call this to draw
- void (*resized)(GlxSize*); // call this when resized
- Widget w; // the GlxMDraw widget
- float x, y; // widget specific info
- };
-
- static RadioSetting radioState; // radio radio state
- static SoundSetting soundState; // sound radio state
- static char* msgBuffer; // current messages;
- static Widget msgText; // message text widget
- static Widget scoreInfo, // score display widget
- playersInfo, // num players display widget
- flagInfo; // whose flag display widget
- static Widget radioToggle[3], // radio radio toggle buttons
- soundToggle[3]; // sound radio toggle buttons
- static float thrustLevel[11]; // thrust amounts
-
- // GlxMDraw widget draw and resize function prototypes
- static void drawWeapon(GlxSize*, int);
- static void resizeWeapon(GlxSize*);
- static void drawShield(GlxSize*, int);
- static void resizeShield(GlxSize*);
- static void drawLaser(GlxSize*, int);
- static void resizeLaser(GlxSize*);
- static void drawMissile(GlxSize*, int);
- static void resizeMissile(GlxSize*);
- static void drawThrust(GlxSize*, int);
- static void resizeThrust(GlxSize*);
- static void drawFuel(GlxSize*, int);
- static void resizeFuel(GlxSize*);
- static void drawRadar(GlxSize*, int);
- static void resizeRadar(GlxSize*);
- static void drawTarget(GlxSize*, int);
- static void drawTeamColor(GlxSize*, int);
- static void unitInitCallback(Widget w, caddr_t d);
- static void unitResizeCallback(Widget w, caddr_t d, caddr_t cd);
- static void glxDrawCallback(Widget w, caddr_t d, caddr_t);
-
- // GlxMDraw widget sizes and resize function prototypes
- static GlxSize weaponSize = { 100, 100, drawWeapon, resizeWeapon },
- shieldSize = { 100, 100, drawShield, resizeShield },
- laserSize = { 1, 1, drawLaser, resizeLaser },
- missileSize = { 1, 1, drawMissile, resizeMissile },
- thrustSize = { 1, 1, drawThrust, resizeThrust },
- fuelSize = { 1, 1, drawFuel, resizeFuel },
- radarSize = { PANELHEIGHT-40, PANELHEIGHT-40,
- drawRadar, resizeRadar },
- targetSize = { 100, 120, drawTarget, 0 },
- colorSize = { 100, 100, drawTeamColor, 0 };
-
- // old state information (to avoid excessive redrawing and computation)
- static int oldShield[6],
- oldLaser,
- oldMissile,
- oldMissileReady,
- oldThrust,
- oldFuel,
- blinkOn,
- oldTargetHeading,
- oldTargetPitch,
- oldTargetDist;
- static ShipObject* oldTarget;
- static float blinkTime;
-
- // key state information
- static int thrustKeyPressed[11];
-
- //
- // Begin control panel callback functions
- //
-
- //
- // general GL callbacks
- //
-
- static void unitInitCallback(Widget w, caddr_t d)
- {
- GLXwinset(XtDisplay(w), XtWindow(w));
- ortho2(0.0, 1.0, 0.0, 1.0);
- subpixel(TRUE);
- GlxSize* s = (GlxSize*)d;
- if (s) {
- XtVaGetValues(w,
- XmNwidth, &s->dx,
- XmNheight, &s->dy,
- NULL);
- if (s->resized) (*s->resized)(s);
- }
- }
-
- static void unitResizeCallback(Widget w, caddr_t d, caddr_t cd)
- {
- GlxDrawCallbackStruct* calldata = (GlxDrawCallbackStruct*)cd;
- GLXwinset(XtDisplay(w), XtWindow(w));
- viewport(0, (Scoord) calldata->width-1, 0, (Scoord) calldata->height-1);
- ortho2(0.0, 1.0, 0.0, 1.0);
- GlxSize* s = (GlxSize*)d;
- if (s) {
- s->dx = calldata->width;
- s->dy = calldata->height;
- if (s->resized) (*s->resized)(s);
- if (s->draw) (*s->draw)(s, TRUE);
- }
- }
-
- static void pixelInitCallback(Widget w, caddr_t d, caddr_t)
- {
- GLXwinset(XtDisplay(w), XtWindow(w));
- subpixel(TRUE);
- Dimension dx, dy;
- XtVaGetValues(w,
- XmNwidth, &dx,
- XmNheight, &dy,
- NULL);
- ortho2(-0.5, float(dx) - 0.5, -0.5, float(dy) - 0.5);
- GlxSize* s = (GlxSize*)d;
- if (s) {
- s->dx = dx;
- s->dy = dy;
- if (s->resized) (*s->resized)(s);
- }
- }
-
- static void pixelResizeCallback(Widget w, caddr_t d, caddr_t cd)
- {
- GlxDrawCallbackStruct* calldata = (GlxDrawCallbackStruct*)cd;
- GLXwinset(XtDisplay(w), XtWindow(w));
- viewport(0, (Scoord) calldata->width-1, 0, (Scoord) calldata->height-1);
- ortho2(-0.5, float(calldata->width)-0.5, -0.5, float(calldata->height)-0.5);
- GlxSize* s = (GlxSize*)d;
- if (s) {
- s->dx = calldata->width;
- s->dy = calldata->height;
- if (s->resized) (*s->resized)(s);
- if (s->draw) (*s->draw)(s, TRUE);
- }
- }
-
- static void radarInitCallback(Widget w, caddr_t d, caddr_t)
- {
- GLXwinset(XtDisplay(w), XtWindow(w));
- ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
- subpixel(TRUE);
- GlxSize* s = (GlxSize*)d;
- if (s) {
- XtVaGetValues(w,
- XmNwidth, &s->dx,
- XmNheight, &s->dy,
- NULL);
- if (s->resized) (*s->resized)(s);
- }
- }
-
- static void radarResizeCallback(Widget w, caddr_t d, caddr_t cd)
- {
- GlxDrawCallbackStruct* calldata = (GlxDrawCallbackStruct*)cd;
- GLXwinset(XtDisplay(w), XtWindow(w));
- viewport(0, (Scoord) calldata->width-1, 0, (Scoord) calldata->height-1);
- ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
- GlxSize* s = (GlxSize*)d;
- if (s) {
- s->dx = calldata->width;
- s->dy = calldata->height;
- if (s->resized) (*s->resized)(s);
- if (s->draw) (*s->draw)(s, TRUE);
- }
- }
-
- static void glxDrawCallback(Widget w, caddr_t d, caddr_t)
- {
- GlxSize* s = (GlxSize*)d;
- if (s && s->draw) (*s->draw)(s, TRUE);
- else {
- GLXwinset(XtDisplay(w), XtWindow(w));
- cpack(0xff000000);
- clear();
- }
- }
-
- //
- // Stylized ship
- //
-
- static float stylizedShip[9][2] = {
- { 0.0, 1.0 },
- { -0.4, -1.0 },
- { 0.4, -1.0 },
- { -0.2, 0.0 },
- { -0.9, -0.6 },
- { -1.0, -1.0 },
- { 0.2, 0.0 },
- { 0.9, -0.6 },
- { 1.0, -1.0 } };
-
- static void drawStylizedShip()
- {
- bgnclosedline();
- v2f(stylizedShip[0]);
- v2f(stylizedShip[1]);
- v2f(stylizedShip[2]);
- endclosedline();
- bgnline();
- v2f(stylizedShip[3]);
- v2f(stylizedShip[4]);
- v2f(stylizedShip[5]);
- v2f(stylizedShip[1]);
- endline();
- bgnline();
- v2f(stylizedShip[6]);
- v2f(stylizedShip[7]);
- v2f(stylizedShip[8]);
- v2f(stylizedShip[2]);
- endline();
- }
-
- //
- // Weapon callbacks
- //
-
- static float laserInfoPos[2] = { 0.0, 1.0 };
- static float missileInfoPos[2][2] =
- { { -0.5, -0.5 }, { 0.5, -0.5 } };
-
- static void drawWeapon(GlxSize* s, int)
- {
- GLXwinset(XtDisplay(s->w), XtWindow(s->w));
- cpack(0xff000000);
- clear();
- cpack(0xff808080);
- pushmatrix();
- translate(0.5, 0.50, 0.0);
- scale(0.25, 0.25, 0.25);
- drawStylizedShip();
-
- // draw laser
- if (currentWeapon() == Laser) cpack(0xff00ff00);
- else cpack(0xff808080);
- sboxf(laserInfoPos[0] - 12.0*s->x, laserInfoPos[1] - 12.0*s->y,
- laserInfoPos[0] + 12.0*s->x, laserInfoPos[1] + 12.0*s->y);
-
- // draw missiles
- if (currentWeapon() == Missile) cpack(0xff00ff00);
- else cpack(0xff808080);
- for (int i = 0; i < 2; i++)
- sboxf(missileInfoPos[i][0] - 4.0*s->x, missileInfoPos[i][1] - 0.5,
- missileInfoPos[i][0] + 4.0*s->x, missileInfoPos[i][1] + 0.5);
- popmatrix();
-
- // show weapon name
- font(0);
- cpack(0xffc0c0c0);
- cmov2(4.0 * s->x, float(getdescender() + 1) * s->y);
- charstr(weaponName(currentWeapon()));
- }
-
- static void resizeWeapon(GlxSize* s)
- {
- s->x = 1.0 / float(s->dx);
- s->y = 1.0 / float(s->dy);
- }
-
- static void inputWeapon(Widget, caddr_t d, caddr_t cd)
- {
- // if it's a left button press, select weapon mouse is nearer
- GlxDrawCallbackStruct* calldata = (GlxDrawCallbackStruct*)cd;
- if (calldata->event->xany.type == ButtonPress &&
- calldata->event->xbutton.button == Button1) {
- // very simple check -- if in upper part select laser else missiles
- GlxSize* s = (GlxSize*)d;
- if (calldata->event->xbutton.y < int(0.5 * float(s->dy))) {
- currentWeapon(Laser);
- }
- else {
- currentWeapon(Missile);
- }
- }
- }
-
- //
- // Shield callbacks
- //
-
- static short shieldInfoPos[6][2];
- static short shieldInfoSize[2];
-
- static void drawShield(GlxSize* s, int forced)
- {
- int strengthChanges = 0, strength[6];
- // get strengths and see if they've changed
- for (int i = ShipObject::FrontShield; i <= ShipObject::BottomShield; i++) {
- strength[i] = int(20.0 * myShip->shipInfo().shieldStrength[i] + 0.5);
- if (oldShield[i] != strength[i]) strengthChanges++;
- }
- if (!forced && strengthChanges == 0) return; // no difference
-
- GLXwinset(XtDisplay(s->w), XtWindow(s->w));
- if (forced || strengthChanges == 6) { // redraw whole thing
- cpack(0xff000000);
- clear();
- cpack(0xff808080);
- pushmatrix();
- translate(float(s->dx >> 1), float(s->dy >> 1), 0.0);
- // keep scale uniform so the ship isn't distorted
- scale(0.25 * float(s->dx), 0.25 * float(s->dx), 0.25 * float(s->dx));
- drawStylizedShip();
- popmatrix();
- }
-
- for (i = ShipObject::FrontShield; i <= ShipObject::BottomShield; i++) {
- if (!forced && oldShield[i] == strength[i]) continue;
-
- // record new strength
- oldShield[i] = strength[i];
-
- // set color according to remaining strength
- if (strength[i] < 3) cpack(0xff0000ff);
- else if (strength[i] < 6) cpack(0xff00dddd);
- else cpack(0xff00bb00);
-
- // draw box
- if (strength[i] == 0 && blinkOn) { // solid warning bar
- sboxfs(shieldInfoPos[i][0], shieldInfoPos[i][1] - 1,
- shieldInfoPos[i][0] + shieldInfoSize[0] - 1,
- shieldInfoPos[i][1] + shieldInfoSize[1]);
- continue;
- }
- sboxs(shieldInfoPos[i][0], shieldInfoPos[i][1] - 1,
- shieldInfoPos[i][0] + shieldInfoSize[0] - 1,
- shieldInfoPos[i][1] + shieldInfoSize[1]);
-
- // draw shield strength meter
- for (int j = 0; j < strength[i]; j++)
- sboxfs(shieldInfoPos[i][0] + 1,
- shieldInfoPos[i][1] + (Scoord)(float(j) * s->y + 0.5),
- shieldInfoPos[i][0] + shieldInfoSize[0] - 2,
- shieldInfoPos[i][1] + (Scoord)(float(j+1) * s->y + 0.5) - 1);
-
- // clear remaining space in meter
- cpack(0xff000000);
- for (; j < 20; j++)
- sboxfs(shieldInfoPos[i][0] + 1,
- shieldInfoPos[i][1] + (Scoord)(float(j) * s->y + 0.5),
- shieldInfoPos[i][0] + shieldInfoSize[0] - 2,
- shieldInfoPos[i][1] + (Scoord)(float(j+1) * s->y + 0.5) - 1);
- }
- }
-
- static void resizeShield(GlxSize* s)
- {
- shieldInfoSize[0] = 12;
- shieldInfoSize[1] = short(0.25 * float(s->dx) + 0.5);
-
- s->y = float(shieldInfoSize[1]) / 20.0;
-
- shieldInfoPos[ShipObject::FrontShield][0] = (s->dx - shieldInfoSize[0]) >> 1;
- shieldInfoPos[ShipObject::FrontShield][1] = s->dy - shieldInfoSize[1] - 4;
-
- shieldInfoPos[ShipObject::RearShield][0] = (s->dx - shieldInfoSize[0]) >> 1;
- shieldInfoPos[ShipObject::RearShield][1] = 4;
-
- shieldInfoPos[ShipObject::TopShield][0] = s->dx - shieldInfoSize[0] - 4;
- shieldInfoPos[ShipObject::TopShield][1] = s->dy - shieldInfoSize[1] - 4;
-
- shieldInfoPos[ShipObject::BottomShield][0] = 4;
- shieldInfoPos[ShipObject::BottomShield][1] = 4;
-
- shieldInfoPos[ShipObject::LeftShield][0] =
- shieldInfoPos[ShipObject::BottomShield][0] +
- ((shieldInfoPos[ShipObject::RearShield][0] -
- shieldInfoPos[ShipObject::BottomShield][0]) >> 1);
- shieldInfoPos[ShipObject::LeftShield][1] = (s->dy - shieldInfoSize[1]) >> 1;
-
- shieldInfoPos[ShipObject::RightShield][0] =
- shieldInfoPos[ShipObject::FrontShield][0] +
- ((shieldInfoPos[ShipObject::TopShield][0] -
- shieldInfoPos[ShipObject::FrontShield][0]) >> 1);
- shieldInfoPos[ShipObject::RightShield][1] = (s->dy - shieldInfoSize[1]) >> 1;
- }
-
- //
- // Laser callbacks
- //
-
- static void drawLaser(GlxSize* s, int forced)
- {
- int t = int(10.0 * (0.1 + 0.7 * myShip->laserHeat()));
- if (t < 0) t = 0;
- else if (t > 10) t = 10;
- if (!forced && t == oldLaser) return; // not enough difference
- oldLaser = t;
-
- GLXwinset(XtDisplay(s->w), XtWindow(s->w));
- cpack(0xff000000);
- clear();
- for (int i = 0; i < t; i++) {
- if (i == 0) cpack(0xff00bb00);
- else if (i == 5) cpack(0xff00dddd);
- else if (i == 7) cpack(0xff0000ff);
- sboxfs((Scoord)(float(i) * s->x + 0.5), 0,
- (Scoord)(float(i+1) * s->x + 0.5) - 2, s->dy-1);
- }
- }
-
- static void resizeLaser(GlxSize* s)
- {
- s->x = float(s->dx) / 10.0;
- }
-
- //
- // Missile callbacks
- //
-
- static void drawMissile(GlxSize* s, int forced)
- {
- int t = myShip->numMissiles();
- if (t < 0) t = 0;
- else if (t > 16) t = 16;
- int tr = myShip->missileReady();
- if (!forced && t == oldMissile && tr == oldMissileReady)
- return; // no difference
- oldMissile = t;
- oldMissileReady = tr;
-
- GLXwinset(XtDisplay(s->w), XtWindow(s->w));
- cpack(0xff000000);
- clear();
- if (tr) cpack(0xff00bb00);
- else cpack(0xff00dddd);
- for (int i = 0; i < t && i < 15; i++)
- sboxfs((Scoord)(float(i) * s->x + 0.5), 0,
- (Scoord)(float(i+1) * s->x + 0.5) - 2, s->dy-1);
- if (t == 16) {
- Scoord v[2];
- bgnpolygon();
- v[0] = (Scoord)(float(i) * s->x + 0.5); v[1] = s->dy-1; v2s(v);
- v[1] = 0; v2s(v);
- v[0] += 9; v[1] = s->dy >> 1; v2s(v);
- endpolygon();
- }
- }
-
- static void resizeMissile(GlxSize* s)
- {
- s->x = float(s->dx - 10) / 15.0;
- }
-
- //
- // Thrust callbacks
- //
-
- static void drawThrust(GlxSize* s, int forced)
- {
- int t = int(10.0 * myShip->engineOutput() + 0.5);
- if (t < -10) t = -10;
- else if (t > 10) t = 10;
- if (!forced && t == oldThrust) return; // no difference
- oldThrust = t;
-
- GLXwinset(XtDisplay(s->w), XtWindow(s->w));
- cpack(0xff000000);
- clear();
- if (t >= 0)
- cpack(0xff00bb00);
- else
- cpack(0xff0000bb);
- for (int i = 0; i < abs(t); i++)
- sboxfs((Scoord)(float(i) * s->x + 0.5), 0,
- (Scoord)(float(i+1) * s->x + 0.5) - 2, s->dy-1);
- }
-
- static void resizeThrust(GlxSize* s)
- {
- s->x = float(s->dx) / 10.0;
- }
-
- //
- // Fuel callbacks
- //
-
- static void drawFuel(GlxSize* s, int forced)
- {
- int t = int(20.0 * myShip->fuelLeft() + 0.5);
- if (t < 0) t = 0;
- else if (t > 20) t = 20;
- if (!forced && t == oldFuel) return; // no difference
- oldFuel = t;
-
- GLXwinset(XtDisplay(s->w), XtWindow(s->w));
- cpack(0xff000000);
- clear();
- if (t > 7) cpack(0xff00bb00);
- else if (t > 3) cpack(0xff00dddd);
- else cpack(0xff0000ff);
- for (int i = 0; i < t; i++)
- sboxfs((Scoord)(float(i) * s->x + 0.5), 0,
- (Scoord)(float(i+1) * s->x + 0.5) - 2, s->dy-1);
- }
-
- static void resizeFuel(GlxSize* s)
- {
- s->x = float(s->dx) / 20.0;
- }
-
- //
- // Radio callbacks
- //
-
- static void radioChanged(Widget, caddr_t u,
- XmToggleButtonCallbackStruct* b)
- {
- if (b->reason == XmCR_VALUE_CHANGED && b->set)
- radioState = RadioSetting(u);
- }
-
- //
- // Sound callbacks
- //
-
- static void soundChanged(Widget, caddr_t u,
- XmToggleButtonCallbackStruct* b)
- {
- if (b->reason == XmCR_VALUE_CHANGED && b->set)
- soundState = SoundSetting(u);
- }
-
- //
- // Radar callbacks
- //
-
- static void radarRangeChanged(Widget, caddr_t u,
- XmArrowButtonCallbackStruct* b)
- {
- if (b->reason == XmCR_ACTIVATE) {
- switch (int(u)) {
- case 0: decreaseRange(); break;
- case 1: increaseRange(); break;
- }
- }
- }
-
- static void drawRadar(GlxSize* s, int)
- {
- GLXwinset(XtDisplay(s->w), XtWindow(s->w));
- cpack(0xff000000);
- clear();
- zclear();
-
- // draw radar objects
- ShipObject* ship, *targ;
- targ = (currentTarget() == -1) ? NULL : getTarget(currentTarget());
- float r = 1.0 / radarRange();
- float x = s->x / r, y = s->y / r;
-
- pushmatrix();
- scale(r, r, r);
-
- // draw all ships and missiles
- for (int i = 0; i < numberPlayers(); i++) {
- ship = getPlayer(i);
- ShipInfo& info = ship->shipInfo();
- if (ship->active() != ObjectActive) continue;
- if (i != 0) {
- cpack(teamColor(ship->team()));
- pushmatrix();
- translate(playerView[i][0].lp[0], -playerView[i][0].lp[2],
- playerView[i][0].lp[1]);
- if (playerView[i][0].lp[1] < 0.0)
- rectf(-x, -y, x, y);
- else
- rectf(-x, -y, 2.0*x, 2.0*y);
-
- if (ship == targ) { // highlight if current target
- cpack(0xffffffff);
- rect(-4.0*x, -4.0*y, 4.0*x, 4.0*y);
- }
-
- if (ship->flag() != NoTeam) { // draw flag if it has one
- cpack(teamColor(ship->flag()));
- rectf(0.0, -3.0*y, 0.0, 3.0*y);
- rectf(-3.0*x, 0.0, 3.0*x, 0.0);
- }
- popmatrix();
- }
-
- cpack(0xffffffff);
- for (int j = 0; j < MAXMISSILES; j++) {
- if (info.missile[j].active == ObjectActive) {
- pushmatrix();
- translate(playerView[i][j+1].lp[0], -playerView[i][j+1].lp[2],
- playerView[i][j+1].lp[1]);
- if (playerView[i][j+1].lp[1] < 0.0)
- rectf(0.0, 0.0, 0.5*x, 0.5*y);
- else
- rectf(0.0, 0.0, 1.5*x, 0.5*y);
- popmatrix();
- }
- }
- }
-
- // draw circles for bases
- SbVec3f p;
- for (i = 0; i < NUMTEAMS; i++) {
- cpack(teamColor(Team(i)));
- myShip->findLocalPosition(basePosition(Team(i)), p);
- pushmatrix();
- translate(p[0], -p[2], p[1]);
- drawRadarBase(Team(i));
- popmatrix();
- }
-
- // draw asteroids
- cpack(0xff808080);
- for (i = 0; i < numberAsteroids(); i++) {
- pushmatrix();
- translate(asteroidView[i].lp[0], -asteroidView[i].lp[2],
- asteroidView[i].lp[1]);
- drawRadarAsteroid(i);
- popmatrix();
- }
-
- // draw flags not on ships
- for (i = 0; i < NUMTEAMS; i++) {
- if (getTeam(Team(i)).state == FlagReady) {
- cpack(teamColor(Team(i)));
- pushmatrix();
- translate(flagView[i].lp[0], -flagView[i].lp[2], flagView[i].lp[1]);
- rectf(0.0, -3.0*y, 0.0, 3.0*y);
- rectf(-3.0*x, 0.0, 3.0*x, 0.0);
- popmatrix();
- }
- }
-
- popmatrix();
-
- // draw dot for me
- cpack(0xff4080c0);
- sboxf(-2.0*s->x, -2.0*s->y, 2.0*s->x, 2.0*s->y);
- if (myShip->flag() != NoTeam) {
- cpack(teamColor(myShip->flag()));
- sboxf(0.0, -3.0*s->y, 0.0, 3.0*s->y);
- sboxf(-3.0*s->x, 0.0, 3.0*s->x, 0.0);
- }
-
- // draw range
- font(0);
- cpack(0xffdddddd);
- cmov2(-1.0 + 4.0 * s->x, -1.0 + s->y * (4.0 + float(getdescender())));
- char buf[20];
- sprintf(buf, "%d", int(radarRange() / 1000.0));
- charstr(buf);
-
- swapbuffers();
- }
-
- static void resizeRadar(GlxSize* s)
- {
- s->x = 2.0 / float(s->dx);
- s->y = 2.0 / float(s->dy);
- }
-
- //
- // Target callbacks
- //
-
- static void targetNumChanged(Widget, caddr_t u,
- XmArrowButtonCallbackStruct* b)
- {
- if (b->reason == XmCR_ACTIVATE) {
- switch (int(u)) {
- case 0: prevTarget(); break;
- case 1: nextTarget(); break;
- }
- }
- }
-
- static void drawTarget(GlxSize* s, int forced)
- {
- int changed = FALSE, td, th, tp, tNum = currentTarget();
-
- // get target info
- ShipObject* tObj = (tNum == -1) ? NULL : getTarget(tNum);
- if (tObj) {
- SbVec3f tPos = tObj->position();
- tPos -= myShip->position();
- float dist = tPos.length();
- th = int(atan2(tPos[2], tPos[0]) * 180.0 / M_PI + 0.5);
- if (th < 0) th += 360;
- tp = int(asin(tPos[1] / dist) * 180.0 / M_PI + 0.5);
- td = int(dist / 100.0 + 0.5);
- if (!forced && // if not a forced redraw
- oldTarget == tObj && // and same target object
- th == oldTargetHeading && // and same heading
- tp == oldTargetPitch && // and same pitch
- td == oldTargetDist) // and same distance
- return; // then no difference
- }
- else if (!forced && !oldTarget) return; // if still no target, return
- oldTarget = tObj;
-
- GLXwinset(XtDisplay(s->w), XtWindow(s->w));
- cpack(0xff000000); // clear display
- clear();
- if (tNum == -1) return; // no target
-
- oldTargetHeading = th;
- oldTargetPitch = tp;
- oldTargetDist = td;
-
- // draw name, team and class
- font(0);
- short d = short(getdescender()), h = short(getheight()),
- y = short(s->dy) - h + d - 2;
- cpack(0xffc0c0c0);
- cmov2s(2, y);
- charstr(tObj->name());
- cpack(teamColor(tObj->team()) | 0xff7f7f7f);
- cmov2s(2, y -= h);
- charstr(teamName(tObj->team()));
- cpack(0xffc0c0c0);
- cmov2s(2, y -= h);
- charstr(shipClassName(tObj->shipClass()));
-
- // draw heading/pitch to target
- char buf[30];
- sprintf(buf, "%d/%d", th, tp);
- cmov2s(2, y -= h + 4);
- charstr(buf);
-
- // draw range to target
- sprintf(buf, "%d.%d km", td / 10, td % 10);
- cmov2s(2, y -= h);
- charstr(buf);
- }
-
- //
- // Team color box callback
- //
-
- static void drawTeamColor(GlxSize* s, int)
- {
- GLXwinset(XtDisplay(s->w), XtWindow(s->w));
- cpack(teamColor(myShip->team()));
- clear();
- }
-
- //
- // General button callbacks
- //
-
- static void pauseCallback(Widget, caddr_t, caddr_t)
- {
- togglePauseGame();
- }
-
- static void helpCallback(Widget, caddr_t, caddr_t)
- {
- showHelp();
- }
-
- static void quitCallback(Widget, caddr_t, caddr_t)
- {
- quitGame();
- }
-
- //
- // End control panel callback functions
- //
-
- //
- // Begin control panel creation routines
- //
-
- static Widget makeLabel(Widget parent)
- {
- Widget w = XmCreateLabelGadget(parent, "label", NULL, 0);
- XtManageChild(w);
- return w;
- }
-
- static Widget makeInfo(Widget parent)
- {
- Widget w = XmCreateLabelGadget(parent, "info", NULL, 0);
- XtManageChild(w);
- XmString s = XmStringCreateSimple("");
- XtVaSetValues(w, XmNlabelString, s, NULL);
- XmStringFree(s);
- return w;
- }
-
- static Widget makeButton(Widget parent, char* name,
- int userData = 0, XtCallbackProc cb = NULL)
- {
- Widget w = XmCreatePushButtonGadget(parent, name, NULL, 0);
- XtManageChild(w);
- if (cb)
- XtAddCallback(w, XmNactivateCallback, cb, (XtPointer)userData);
- return w;
- }
-
- static Widget makeRadio(Widget parent, char* name,
- int userData = 0, XtCallbackProc cb = NULL)
- {
- Widget w = XmCreateToggleButtonGadget(parent, name, NULL, 0);
- XtManageChild(w);
- XtVaSetValues(w,
- XmNuserData, (void*)userData,
- NULL);
- if (cb)
- XtAddCallback(w, XmNvalueChangedCallback, cb, (XtPointer)userData);
- return w;
- }
-
- static Widget makeArrow(Widget parent, char* name, unsigned char d,
- int userData = 0, XtCallbackProc cb = NULL)
- {
- Widget w = XmCreateArrowButtonGadget(parent, name, NULL, 0);
- XtManageChild(w);
- XtVaSetValues(w,
- XmNarrowDirection, d,
- XmNshadowThickness, 0,
- NULL);
- if (cb)
- XtAddCallback(w, XmNactivateCallback, cb, (XtPointer)userData);
- return w;
- }
-
- static Widget makeBox(Widget parent)
- {
- Widget w = XmCreateFrame(parent, "", NULL, 0);
- XtManageChild(w);
- XtVaSetValues(w,
- XmNshadowType, XmSHADOW_ETCHED_IN,
- XmNmarginWidth, 0,
- XmNmarginHeight, 0,
- NULL);
- return w;
- }
-
- static GLXconfig glxConfig[] =
- { {GLX_NORMAL, GLX_DOUBLE, FALSE},
- {GLX_NORMAL, GLX_RGB, TRUE},
- {0, 0, 0} };
- static GLXconfig glxRConfig[] =
- { {GLX_NORMAL, GLX_DOUBLE, TRUE},
- {GLX_NORMAL, GLX_RGB, TRUE},
- {GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG},
- {0, 0, 0} };
-
- static Widget makeDrawArea(Widget parent, char* name, int dx, int dy)
- {
- Arg args[1];
- GLXconfig* c = GLXgetconfig(XtDisplay(parent),
- XScreenNumberOfScreen(XtScreen(parent)), glxConfig);
- XtSetArg(args[0], GlxNglxConfig, c);
- Widget w = GlxCreateMDraw(parent, name, args, 1);
- XtManageChild(w);
- free(c);
- XtVaSetValues(w,
- XmNwidth, dx,
- XmNheight, dy,
- NULL);
- return w;
- }
-
- static Widget makeUnitDrawArea(Widget parent, char* name,
- GlxSize* size)
- {
- Widget w = makeDrawArea(parent, name, size->dx, size->dy);
- XtAddCallback(w, GlxNginitCallback, (XtCallbackProc)unitInitCallback, (XtPointer)size);
- XtAddCallback(w, GlxNresizeCallback, (XtCallbackProc)unitResizeCallback, (XtPointer)size);
- XtAddCallback(w, GlxNexposeCallback, (XtCallbackProc)glxDrawCallback, (XtPointer)size);
- if (size) size->w = w;
- return w;
- }
-
- static Widget makePixelDrawArea(Widget parent, char* name,
- GlxSize* size)
- {
- Widget w = makeDrawArea(parent, name, size->dx, size->dy);
- XtAddCallback(w, GlxNginitCallback, (XtCallbackProc)pixelInitCallback, (XtPointer)size);
- XtAddCallback(w, GlxNresizeCallback, (XtCallbackProc)pixelResizeCallback, (XtPointer)size);
- XtAddCallback(w, GlxNexposeCallback, (XtCallbackProc)glxDrawCallback, (XtPointer)size);
- if (size) size->w = w;
- return w;
- }
-
- static Widget makeRadarDrawArea(Widget parent, char* name,
- GlxSize* size)
- {
- Arg args[1];
- GLXconfig* c = GLXgetconfig(XtDisplay(parent),
- XScreenNumberOfScreen(XtScreen(parent)), glxRConfig);
- XtSetArg(args[0], GlxNglxConfig, c);
- Widget w = GlxCreateMDraw(parent, name, args, 1);
- XtManageChild(w);
- free(c);
- XtVaSetValues(w,
- XmNwidth, size->dx,
- XmNheight, size->dy,
- NULL);
- XtAddCallback(w, GlxNginitCallback, (XtCallbackProc)radarInitCallback, (XtPointer)size);
- XtAddCallback(w, GlxNresizeCallback, (XtCallbackProc)radarResizeCallback, (XtPointer)size);
- XtAddCallback(w, GlxNexposeCallback, (XtCallbackProc)glxDrawCallback, (XtPointer)size);
- if (size) size->w = w;
- return w;
- }
-
- static Widget makeShipStatus(Widget top)
- {
- // make ship status form (weapon, shield, fuel, and thrust status)
- Widget statusForm = XmCreateForm(top, "status", NULL, 0);
- XtManageChild(statusForm);
-
- // make weapon select and shield readouts
- Widget weaponForm = XmCreateForm(statusForm, "weapons", NULL, 0);
- XtManageChild(weaponForm);
- Widget weaponLabel = makeLabel(weaponForm);
- Widget weaponBox = makeBox(weaponForm);
- Widget shieldForm = XmCreateForm(statusForm, "shields", NULL, 0);
- XtManageChild(shieldForm);
- Widget shieldLabel = makeLabel(shieldForm);
- Widget shieldBox = makeBox(shieldForm);
- XtVaSetValues(weaponForm,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(weaponLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(weaponBox,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNbottomAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, weaponLabel,
- XmNleftWidget, weaponLabel,
- NULL);
- XtVaSetValues(shieldForm,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNtopWidget, weaponForm,
- XmNbottomWidget, weaponForm,
- XmNleftWidget, weaponForm,
- XmNleftOffset, 8,
- NULL);
- XtVaSetValues(shieldLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(shieldBox,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNbottomAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, shieldLabel,
- XmNleftWidget, shieldLabel,
- NULL);
- XtAddCallback(makeUnitDrawArea(weaponBox, "info", &weaponSize),
- GlxNinputCallback, (XtCallbackProc)inputWeapon, (XtPointer)&weaponSize);
- makePixelDrawArea(shieldBox, "info", &shieldSize);
-
- // make laser temp., missile count, thrust level, and fuel level readouts
- Widget laserForm = XmCreateForm(statusForm, "laser", NULL, 0);
- XtManageChild(laserForm);
- Widget laserLabel = makeLabel(laserForm);
- Widget laserBox = makeBox(laserForm);
- Widget missileForm = XmCreateForm(statusForm, "missile", NULL, 0);
- XtManageChild(missileForm);
- Widget missileLabel = makeLabel(missileForm);
- Widget missileBox = makeBox(missileForm);
- Widget thrustForm = XmCreateForm(statusForm, "thrust", NULL, 0);
- XtManageChild(thrustForm);
- Widget thrustLabel = makeLabel(thrustForm);
- Widget thrustBox = makeBox(thrustForm);
- Widget fuelForm = XmCreateForm(statusForm, "fuel", NULL, 0);
- XtManageChild(fuelForm);
- Widget fuelLabel = makeLabel(fuelForm);
- Widget fuelBox = makeBox(fuelForm);
-
- XtVaSetValues(laserForm,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, weaponForm,
- XmNleftWidget, weaponForm,
- XmNrightWidget, shieldForm,
- XmNtopOffset, 6,
- NULL);
- XtVaSetValues(laserLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_POSITION,
- NULL);
- XtVaSetValues(laserBox,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, laserLabel,
- XmNbottomWidget, laserLabel,
- XmNleftWidget, laserLabel,
- XmNleftOffset, 8,
- NULL);
- XtVaSetValues(missileForm,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, laserForm,
- XmNleftWidget, laserForm,
- XmNrightWidget, laserForm,
- XmNtopOffset, 6,
- NULL);
- XtVaSetValues(missileLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_POSITION,
- NULL);
- XtVaSetValues(missileBox,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, missileLabel,
- XmNbottomWidget, missileLabel,
- XmNleftWidget, missileLabel,
- XmNleftOffset, 8,
- NULL);
- XtVaSetValues(thrustForm,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, missileForm,
- XmNleftWidget, missileForm,
- XmNrightWidget, missileForm,
- XmNtopOffset, 6,
- NULL);
- XtVaSetValues(thrustLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_POSITION,
- NULL);
- XtVaSetValues(thrustBox,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, thrustLabel,
- XmNbottomWidget, thrustLabel,
- XmNleftWidget, thrustLabel,
- XmNleftOffset, 8,
- NULL);
- XtVaSetValues(fuelForm,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, thrustForm,
- XmNleftWidget, thrustForm,
- XmNrightWidget, thrustForm,
- XmNtopOffset, 6,
- NULL);
- XtVaSetValues(fuelLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_POSITION,
- NULL);
- XtVaSetValues(fuelBox,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, fuelLabel,
- XmNbottomWidget, fuelLabel,
- XmNleftWidget, fuelLabel,
- XmNleftOffset, 8,
- NULL);
-
- makePixelDrawArea(laserBox, "info", &laserSize);
- makePixelDrawArea(missileBox, "info", &missileSize);
- makePixelDrawArea(thrustBox, "info", &thrustSize);
- makePixelDrawArea(fuelBox, "info", &fuelSize);
-
- return statusForm;
- }
-
- static Widget makeRadioControls(Widget top)
- {
- // make radio controls
- Widget radioForm = XmCreateForm(top, "radio", NULL, 0);
- XtManageChild(radioForm);
- Widget radioLabel = makeLabel(radioForm);
- Widget radioRadio = XmCreateRadioBox(radioForm, "", NULL, 0);
- XtManageChild(radioRadio);
- radioToggle[0] = makeRadio(radioRadio, "off", RadioOff,
- (XtCallbackProc)radioChanged);
- radioToggle[1] = makeRadio(radioRadio, "team", RadioTeam,
- (XtCallbackProc)radioChanged);
- radioToggle[2] = makeRadio(radioRadio, "all", RadioAll,
- (XtCallbackProc)radioChanged);
- XtVaSetValues(radioLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(radioRadio,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, radioLabel,
- XmNleftWidget, radioLabel,
- NULL);
-
- // initialize toggle buttons
- XmToggleButtonGadgetSetState(radioToggle[0], TRUE, TRUE);
-
- return radioForm;
- }
-
- static Widget makeSoundControls(Widget top)
- {
- // make sound controls
- Widget soundForm = XmCreateForm(top, "sound", NULL, 0);
- XtManageChild(soundForm);
- Widget soundLabel = makeLabel(soundForm);
- Widget soundRadio = XmCreateRadioBox(soundForm, "", NULL, 0);
- XtManageChild(soundRadio);
- soundToggle[0] = makeRadio(soundRadio, "off", SoundOff,
- (XtCallbackProc)soundChanged);
- soundToggle[1] = makeRadio(soundRadio, "warning", SoundWarnings,
- (XtCallbackProc)soundChanged);
- soundToggle[2] = makeRadio(soundRadio, "all", SoundAll,
- (XtCallbackProc)soundChanged);
- XtVaSetValues(soundLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(soundRadio,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, soundLabel,
- XmNleftWidget, soundLabel,
- NULL);
-
- // initialize toggle buttons
- XmToggleButtonGadgetSetState(soundToggle[0], TRUE, TRUE);
-
- return soundForm;
- }
-
- static Widget makeRadar(Widget top)
- {
- // make radar form
- Widget radar = XmCreateForm(top, "radar", NULL, 0);
- XtManageChild(radar);
-
- // make radar controls
- Widget radarLabel = makeLabel(radar);
- Widget radarUp = makeArrow(radar, "down", XmARROW_DOWN, 0,
- (XtCallbackProc)radarRangeChanged);
- Widget radarDown = makeArrow(radar, "up", XmARROW_UP, 1,
- (XtCallbackProc)radarRangeChanged);
- Widget radarBox = makeBox(radar);
- XtVaSetValues(radarLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(radarUp,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_WIDGET,
- XmNtopWidget, radarLabel,
- XmNbottomWidget, radarLabel,
- XmNrightWidget, radarDown,
- NULL);
- XtVaSetValues(radarDown,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, radarLabel,
- XmNbottomWidget, radarLabel,
- XmNrightWidget, radarBox,
- NULL);
- XtVaSetValues(radarBox,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, radarLabel,
- XmNleftWidget, radarLabel,
- NULL);
- makeRadarDrawArea(radarBox, "info", &radarSize);
-
- return radar;
- }
-
- static Widget makeTargetStatus(Widget top)
- {
- // make target form
- Widget targetStatus = XmCreateForm(top, "target", NULL, 0);
- XtManageChild(targetStatus);
-
- // make target controls
- Widget targetLabel = makeLabel(targetStatus);
- Widget targetPrev = makeArrow(targetStatus, "prev", XmARROW_LEFT, 0,
- (XtCallbackProc)targetNumChanged);
- Widget targetNext = makeArrow(targetStatus, "next", XmARROW_RIGHT, 1,
- (XtCallbackProc)targetNumChanged);
- Widget targetBox = makeBox(targetStatus);
- XtVaSetValues(targetLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(targetPrev,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_WIDGET,
- XmNtopWidget, targetLabel,
- XmNbottomWidget, targetLabel,
- XmNrightWidget, targetNext,
- NULL);
- XtVaSetValues(targetNext,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, targetLabel,
- XmNbottomWidget, targetLabel,
- XmNrightWidget, targetBox,
- NULL);
- XtVaSetValues(targetBox,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, targetLabel,
- XmNleftWidget, targetLabel,
- NULL);
- makePixelDrawArea(targetBox, "info", &targetSize);
-
- return targetStatus;
- }
-
- static Widget makeGeneralInfo(Widget top)
- {
- // make general info layout
- Widget info = XmCreateForm(top, "", NULL, 0);
- XtManageChild(info);
-
- // make general info readouts
- Widget score = XmCreateForm(info, "score", NULL, 0);
- XtManageChild(score);
- Widget scoreLabel = makeLabel(score);
- scoreInfo = makeInfo(score);
- Widget players = XmCreateForm(info, "players", NULL, 0);
- XtManageChild(players);
- Widget playersLabel = makeLabel(players);
- playersInfo = makeInfo(players);
- Widget flag = XmCreateForm(info, "flag", NULL, 0);
- XtManageChild(flag);
- Widget flagLabel = makeLabel(flag);
- flagInfo = makeInfo(flag);
- XtVaSetValues(score,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(scoreLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(scoreInfo,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNtopWidget, scoreLabel,
- XmNbottomWidget, scoreLabel,
- XmNleftWidget, scoreLabel,
- XmNleftOffset, 8,
- NULL);
- XtVaSetValues(players,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, score,
- NULL);
- XtVaSetValues(playersLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(playersInfo,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNtopWidget, playersLabel,
- XmNbottomWidget, playersLabel,
- XmNleftWidget, playersLabel,
- XmNleftOffset, 8,
- NULL);
- XtVaSetValues(flag,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNbottomAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, players,
- NULL);
- XtVaSetValues(flagLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(flagInfo,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNtopWidget, flagLabel,
- XmNbottomWidget, flagLabel,
- XmNleftWidget, flagLabel,
- XmNleftOffset, 8,
- NULL);
-
- return info;
- }
-
- static Widget makeMessages(Widget top)
- {
- // make messges form
- Widget messages = XmCreateForm(top, "message", NULL, 0);
- XtManageChild(messages);
-
- // make messges stuff
- if (msgBuffer) free(msgBuffer);
- Widget msgLabel = makeLabel(messages);
- Arg args[5];
- XtSetArg(args[0], XmNeditable, FALSE);
- XtSetArg(args[1], XmNeditMode, XmMULTI_LINE_EDIT);
- XtSetArg(args[2], XmNwordWrap, FALSE);
- XtSetArg(args[3], XmNcursorPositionVisible, FALSE);
- XtSetArg(args[4], XmNhighlightThickness, 0);
- msgText = XmCreateScrolledText(messages, "info", args, 5);
- XtManageChild(msgText);
- msgBuffer = (char*) malloc(sizeof(char) * 81 * MAXMESSAGES);
- memset(msgBuffer, ' ', 81 * MAXMESSAGES);
- for (int i = 0; i < MAXMESSAGES-1; i++)
- msgBuffer[i * 81 + 80] = '\n';
- msgBuffer[i * 81 + 80] = 0;
- XmTextSetString(msgText, msgBuffer);
- XmTextShowPosition(msgText, 81 * (MAXMESSAGES-1));
-
- XtVaSetValues(msgLabel,
- XmNtopAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- NULL);
- XtVaSetValues(XtParent(msgText),
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNbottomAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, msgLabel,
- XmNscrollBarPlacement, XmBOTTOM_RIGHT,
- XmNheight, 144,
- NULL);
-
- // no highlight around scrollbars
- Widget sb;
- XtVaGetValues(XtParent(msgText), XmNhorizontalScrollBar, &sb, NULL);
- if (sb) {
- XtVaSetValues(sb,
- XmNhighlightThickness, 0,
- XmNheight, 18,
- NULL);
- }
- XtVaGetValues(XtParent(msgText), XmNverticalScrollBar, &sb, NULL);
- if (sb) {
- XtVaSetValues(sb,
- XmNhighlightThickness, 0,
- XmNwidth, 18,
- NULL);
- }
-
- return messages;
- }
-
- static Widget makeGeneralButtons(Widget top)
- {
- // make general button layout
- Widget general = XmCreateForm(top, "", NULL, 0);
- XtManageChild(general);
-
- // make general buttons
- Widget pauseButton = makeButton(general, "pause", 0, (XtCallbackProc)pauseCallback);
- Widget helpButton = makeButton(general, "help", 0, (XtCallbackProc)helpCallback);
- Widget quitButton = makeButton(general, "quit", 0, (XtCallbackProc)quitCallback);
- XtVaSetValues(pauseButton,
- XmNtopAttachment, XmATTACH_FORM,
- XmNrightAttachment, XmATTACH_FORM,
- XmNwidth, 75,
- NULL);
- XtVaSetValues(quitButton,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, pauseButton,
- XmNleftWidget, pauseButton,
- XmNrightWidget, pauseButton,
- XmNtopOffset, 6,
- NULL);
- XtVaSetValues(helpButton,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_WIDGET,
- XmNtopWidget, quitButton,
- XmNbottomWidget, quitButton,
- XmNrightWidget, quitButton,
- XmNrightOffset, 8,
- XmNwidth, 75,
- NULL);
-
- return general;
- }
-
- static Widget makeTeamColor(Widget top)
- {
- Widget colorBox = makeBox(top);
- makeUnitDrawArea(colorBox, "info", &colorSize);
- return colorBox;
- }
-
- //
- // End control panel creation routines
- //
-
- //
- // external control panel functions
- // (okay, so makeControlPanel() is still a panel creation routine phhtttt)
- //
-
- Widget makeControlPanel(Widget toplevel)
- {
- Widget controls = XmCreateForm(toplevel, "controls", NULL, 0);
-
- Widget shipStatus = makeShipStatus(controls);
- XtVaSetValues(shipStatus,
- XmNtopAttachment, XmATTACH_FORM,
- XmNbottomAttachment, XmATTACH_FORM,
- XmNleftAttachment, XmATTACH_FORM,
- XmNtopOffset, 4,
- XmNbottomOffset, 8,
- XmNleftOffset, 8,
- NULL);
-
- #ifndef NORADIO
- Widget radioControls = makeRadioControls(controls);
- XtVaSetValues(radioControls,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNtopWidget, shipStatus,
- XmNleftWidget, shipStatus,
- XmNleftOffset, 8,
- NULL);
- #endif
-
- Widget soundControls = makeSoundControls(controls);
- XtVaSetValues(soundControls,
- #ifndef NORADIO
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, radioControls,
- XmNbottomWidget, shipStatus,
- XmNleftWidget, radioControls,
- XmNrightWidget, radioControls,
- #else
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNtopWidget, shipStatus,
- XmNleftWidget, shipStatus,
- XmNleftOffset, 8,
- #endif
- NULL);
-
- Widget radar = makeRadar(controls);
- XtVaSetValues(radar,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNtopWidget, shipStatus,
- XmNbottomWidget, shipStatus,
- #ifndef NORADIO
- XmNleftWidget, radioControls,
- #else
- XmNleftWidget, soundControls,
- #endif
- XmNleftOffset, 8,
- NULL);
-
- Widget target = makeTargetStatus(controls);
- XtVaSetValues(target,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNtopWidget, shipStatus,
- XmNleftWidget, radar,
- XmNleftOffset, 8,
- NULL);
-
- Widget info = makeGeneralInfo(controls);
- XtVaSetValues(info,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, target,
- XmNbottomWidget, shipStatus,
- XmNleftWidget, target,
- XmNrightWidget, target,
- XmNtopOffset, 8,
- NULL);
-
- Widget messages = makeMessages(controls);
- XtVaSetValues(messages,
- XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_WIDGET,
- XmNrightAttachment, XmATTACH_FORM,
- XmNtopWidget, target,
- XmNleftWidget, target,
- XmNleftOffset, 8,
- XmNrightOffset, 8,
- NULL);
-
- Widget general = makeGeneralButtons(controls);
- XtVaSetValues(general,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNrightAttachment, XmATTACH_FORM,
- XmNbottomWidget, shipStatus,
- XmNrightOffset, 8,
- NULL);
-
- Widget teamColor = makeTeamColor(controls);
- XtVaSetValues(teamColor,
- XmNtopAttachment, XmATTACH_WIDGET,
- XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
- XmNtopWidget, messages,
- XmNbottomWidget, shipStatus,
- XmNleftWidget, messages,
- XmNtopOffset, 6,
- NULL);
-
- return controls;
- }
-
- RadioSetting radioDial()
- {
- return radioState;
- }
-
- void radioDial(RadioSetting r)
- {
- if (radioState != r) {
- radioState = r;
- XmToggleButtonGadgetSetState(radioToggle[int(r)], TRUE, TRUE);
- }
- }
-
- SoundSetting soundDial()
- {
- return soundState;
- }
-
- void soundDial(SoundSetting s)
- {
- if (soundState != s) {
- soundState = s;
- XmToggleButtonGadgetSetState(soundToggle[int(s)], TRUE, TRUE);
- }
- }
-
- void weaponChanged()
- {
- drawWeapon(&weaponSize, FALSE);
- }
-
- void laserChanged()
- {
- drawLaser(&laserSize, FALSE);
- }
-
- void missileChanged()
- {
- drawMissile(&missileSize, FALSE);
- }
-
- void thrustChanged()
- {
- drawThrust(&thrustSize, FALSE);
- }
-
- void fuelChanged()
- {
- drawFuel(&fuelSize, FALSE);
- }
-
- void shieldsChanged()
- {
- drawShield(&shieldSize, FALSE);
- }
-
- void targetChanged()
- {
- drawTarget(&targetSize, FALSE);
- }
-
- void scoreChanged()
- {
- char buf[10];
- sprintf(buf, "%d", myShip->score());
- XmString s = XmStringCreateSimple(buf);
- XtVaSetValues(scoreInfo, XmNlabelString, s, NULL);
- XmStringFree(s);
- }
-
- void playersChanged()
- {
- char buf[10];
- sprintf(buf, "%d", numberPlayers());
- XmString s = XmStringCreateSimple(buf);
- XtVaSetValues(playersInfo, XmNlabelString, s, NULL);
- XmStringFree(s);
- }
-
- void flagChanged()
- {
- XmString s;
- if (myShip->flag() == NoTeam)
- s = XmStringCreateSimple("---");
- else
- s = XmStringCreateSimple(teamName(myShip->flag()));
- XtVaSetValues(flagInfo, XmNlabelString, s, NULL);
- XmStringFree(s);
- }
-
- void addMessage(char* msg)
- {
- // shift old messages up and add new message
- memmove(msgBuffer, msgBuffer + 81, 81 * (MAXMESSAGES-1) - 1);
- int l = strlen(msg);
- if (l > 80) l = 80;
- memcpy(msgBuffer + 81 * (MAXMESSAGES-1), msg, l);
- if (l < 80) memset(msgBuffer + 81 * (MAXMESSAGES-1) + l, ' ', 80 - l);
-
- // set new text and show bottom line
- XmTextSetString(msgText, msgBuffer);
- XmTextShowPosition(msgText, 81 * (MAXMESSAGES-1));
- }
-
- static int thrustKey(KeySym k, int press)
- {
- int level;
- switch (k) {
- case XK_0: level = 10; break;
- case XK_1: level = 1; break;
- case XK_2: level = 2; break;
- case XK_3: level = 3; break;
- case XK_4: level = 4; break;
- case XK_5: level = 5; break;
- case XK_6: level = 6; break;
- case XK_7: level = 7; break;
- case XK_8: level = 8; break;
- case XK_9: level = 9; break;
- case XK_grave: level = 0; break; // reverse thrust
- default: return FALSE; // not a thrust key
- }
-
- thrustKeyPressed[level] = press;
- for (int i = 10; i >= 0; i--) // find highest thrust key
- if (thrustKeyPressed[i]) { // found one
- myShip->engineOutput(thrustLevel[i]); // set thrust level
- break;
- }
- if (i < 0) myShip->engineOutput(0.0); // no thrust key pressed
-
- return TRUE; // it was a thrust key
- }
-
- int keyEvent(XEvent* e)
- {
- // get system independent key description
- KeySym k = XLookupKeysym(&(e->xkey), 0);
-
- // check if it's a thrust key ('`','1','2','3',...,'0')
- if (thrustKey(k, (e->xany.type == KeyPress))) return TRUE;
-
- // check for weapon select ('w')
- if (e->xany.type == KeyPress && (k == XK_W || k == XK_w)) {
- switch (currentWeapon()) { // switch active weapon
- case Laser: currentWeapon(Missile); break;
- case Missile: currentWeapon(Laser); break;
- }
- return TRUE;
- }
-
- // check for next target ('t')
- if (e->xany.type == KeyPress && (k == XK_T || k == XK_t)) {
- nextTarget();
- return TRUE;
- }
-
- // check for frames per second activation ('/' or '?')
- if (e->xany.type == KeyPress && (k == XK_slash || k == XK_question)) {
- toggleFps();
- return TRUE;
- }
-
- // check for radar range change ('+' or '=' for up, '-' or '_' for down)
- if (e->xany.type == KeyPress && k == XK_equal) {
- increaseRange();
- return TRUE;
- }
- if (e->xany.type == KeyPress && k == XK_minus) {
- decreaseRange();
- return TRUE;
- }
-
- #ifndef NORADIO
- // check for radio button pressed
- if (e->xany.type == KeyPress && (k == XK_r || k == XK_R)) {
- switch (radioState) {
- case RadioOff:
- XmToggleButtonGadgetSetState(radioToggle[1], TRUE, TRUE);
- break;
- case RadioTeam:
- XmToggleButtonGadgetSetState(radioToggle[2], TRUE, TRUE);
- break;
- case RadioAll:
- XmToggleButtonGadgetSetState(radioToggle[0], TRUE, TRUE);
- break;
- }
- return TRUE;
- }
- #endif
-
- // check for sound button pressed
- if (e->xany.type == KeyPress && (k == XK_s || k == XK_S)) {
- switch (soundState) {
- case SoundOff:
- XmToggleButtonGadgetSetState(soundToggle[1], TRUE, TRUE);
- break;
- case SoundWarnings:
- XmToggleButtonGadgetSetState(soundToggle[2], TRUE, TRUE);
- break;
- case SoundAll:
- XmToggleButtonGadgetSetState(soundToggle[0], TRUE, TRUE);
- break;
- }
- return TRUE;
- }
-
- // check for help key (F1)
- if (e->xany.type == KeyPress && k == XK_F1) {
- showHelp();
- return TRUE;
- }
-
- // check for pause key ('p' or 'P')
- if (e->xany.type == KeyPress && (k == XK_p || k == XK_P)) {
- togglePauseGame();
- return TRUE;
- }
-
- // check for quit key (Esc)
- if (e->xany.type == KeyPress && k == XK_Escape) {
- quitGame();
- return TRUE;
- }
-
- // check for HUD color key ('h' or 'H')
- if (e->xany.type == KeyPress && (k == XK_h || k == XK_H)) {
- nextHudColor();
- return TRUE;
- }
-
- // check for space bar for restart
- if (e->xany.type == KeyPress && k == XK_space) {
- if (myShip->active() == ObjectInactive)
- restartSelf();
- else
- myShip->dropFlag();
- return TRUE;
- }
-
- // not one of my keys
- return FALSE; // let someone else use event
- }
-
- void controlPanelAdvance(float dt)
- {
- // update blink time
- blinkTime += dt;
- while (blinkTime > 1.0) blinkTime -= 1.0; // 1 second period
- int blinked = FALSE;
- if (blinkOn && blinkTime > 0.75) {
- blinkTime -= 0.75;
- blinkOn = FALSE;
- blinked = TRUE;
- }
- else if (!blinkOn && blinkTime > 0.25) {
- blinkTime -= 0.25;
- blinkOn = TRUE;
- blinked = TRUE;
- }
-
- // if blinkOn changed draw appropriate areas
- if (blinked) {
- for (int i = ShipObject::FrontShield; i <= ShipObject::BottomShield; i++)
- if (int(20.0 * myShip->shipInfo().shieldStrength[i] + 0.5) == 0)
- oldShield[i] = -1;
- shieldsChanged();
- }
-
- // draw radar
- drawRadar(&radarSize, TRUE);
-
- if (currentTarget() != -1) targetChanged();
- }
-
- void controlPanelReset()
- {
- // reset time dependant stuff (like blink time)
- blinkTime = 0.0;
- blinkOn = TRUE;
-
- // set old states to bogus values so they'll get updated
- for (int i = ShipObject::FrontShield; i <= ShipObject::BottomShield; i++)
- oldShield[i] = -1;
- oldLaser = -1;
- oldMissile = -1;
- oldThrust = -20;
- oldFuel = -1;
- oldTarget = myShip; // impossible target
-
- // clear keyboard state
- for (i = 0; i <= 10; i++) {
- thrustKeyPressed[i] = FALSE;
- thrustLevel[i] = (float)i / 10.0;
- }
- thrustLevel[0] = -0.25;
-
- // fill in other state info
- weaponChanged();
- laserChanged();
- missileChanged();
- thrustChanged();
- fuelChanged();
- shieldsChanged();
- targetChanged();
- scoreChanged();
- playersChanged();
- flagChanged();
- drawTeamColor(&colorSize, TRUE);
- }
-